import Vue from 'vue'
import Vuex from 'vuex'

import app from './modules/app'
import user from './modules/user'

// default router permission control
// import permission from './modules/permission'

// dynamic router permission control (Experimental)
import permission from './modules/async-router'
import getters from './getters'

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    app,
    user,
    permission
  },
  state: {
    catchArr: [], // 保存缓存的列表(D是固定要缓存的组件）
    catchArrTmp: [], // 临时存放
    keepAliveList: [],
    closeMessageList: []
  },
  mutations: {
    // 对指定组件进行动态更改缓存（缓存）--组件调用该方法时，判断该组件是否存在于该缓存数组，无则添加
    iskeepAlive (state, component) {
      !state.catchArr.includes(component) && state.catchArr.push(component)
    },
    iskeepAliveTmp (state, component) {
      !state.catchArrTmp.includes(component) && state.catchArrTmp.push(component)
    },
    checkKeepAlive (state, component) {
      return state.catchArr.includes(component)
    },
    // 对指定组件进行动态更改缓存（不缓存）--组件调用该方法时，从缓存数组中删除对应的组件元素
    noKeepAlive (state, component) {
      const index = state.catchArr.indexOf(component)
      index > -1 && state.catchArr.splice(index, 1)

      const index2 = state.catchArrTmp.indexOf(component)
      index2 > -1 && state.catchArrTmp.splice(index2, 1)
    },
    joinKeepAliveList (state, component) {
      state.keepAliveList.push(component)
        // [component.name] = (component.component)
    },
    closeMessageList (state, obj) {
      state.closeMessageList.push(obj)
    }
  },
  actions: {

  },
  getters
})
